const specials = {
  atlas: 'atlases',
  beef: 'beefs',
  brother: 'brothers',
  cafe: 'cafes',
  child: 'children',
  cookie: 'cookies',
  corpus: 'corpuses',
  cow: 'cows',
  curve: 'curves',
  foe: 'foes',
  ganglion: 'ganglions',
  genie: 'genies',
  genus: 'genera',
  graffito: 'graffiti',
  hoof: 'hoofs',
  loaf: 'loaves',
  man: 'men',
  money: 'monies',
  mongoose: 'mongooses',
  move: 'moves',
  mythos: 'mythoi',
  niche: 'niches',
  numen: 'numina',
  occiput: 'occiputs',
  octopus: 'octopuses',
  opus: 'opuses',
  ox: 'oxen',
  pasta: 'pasta',
  penis: 'penises',
  sex: 'sexes',
  soliloquy: 'soliloquies',
  testis: 'testes',
  trilby: 'trilbys',
  turf: 'turfs',
  wave: 'waves',
  Amoyese: 'Amoyese',
  bison: 'bison',
  Borghese: 'Borghese',
  bream: 'bream',
  breeches: 'breeches',
  britches: 'britches',
  buffalo: 'buffalo',
  cantus: 'cantus',
  carp: 'carp',
  chassis: 'chassis',
  clippers: 'clippers',
  cod: 'cod',
  coitus: 'coitus',
  Congoese: 'Congoese',
  contretemps: 'contretemps',
  corps: 'corps',
  debris: 'debris',
  diabetes: 'diabetes',
  djinn: 'djinn',
  eland: 'eland',
  elk: 'elk',
  equipment: 'equipment',
  Faroese: 'Faroese',
  flounder: 'flounder',
  Foochowese: 'Foochowese',
  gallows: 'gallows',
  Genevese: 'Genevese',
  Genoese: 'Genoese',
  Gilbertese: 'Gilbertese',
  graffiti: 'graffiti',
  headquarters: 'headquarters',
  herpes: 'herpes',
  hijinks: 'hijinks',
  Hottentotese: 'Hottentotese',
  information: 'information',
  innings: 'innings',
  jackanapes: 'jackanapes',
  Kiplingese: 'Kiplingese',
  Kongoese: 'Kongoese',
  Lucchese: 'Lucchese',
  mackerel: 'mackerel',
  Maltese: 'Maltese',
  mews: 'mews',
  moose: 'moose',
  mumps: 'mumps',
  Nankingese: 'Nankingese',
  news: 'news',
  nexus: 'nexus',
  Niasese: 'Niasese',
  Pekingese: 'Pekingese',
  Piedmontese: 'Piedmontese',
  pincers: 'pincers',
  Pistoiese: 'Pistoiese',
  pliers: 'pliers',
  Portuguese: 'Portuguese',
  proceedings: 'proceedings',
  rabies: 'rabies',
  rice: 'rice',
  rhinoceros: 'rhinoceros',
  salmon: 'salmon',
  Sarawakese: 'Sarawakese',
  scissors: 'scissors',
  series: 'series',
  Shavese: 'Shavese',
  shears: 'shears',
  siemens: 'siemens',
  species: 'species',
  swine: 'swine',
  testes: 'testes',
  trousers: 'trousers',
  trout: 'trout',
  tuna: 'tuna',
  Vermontese: 'Vermontese',
  Wenchowese: 'Wenchowese',
  whiting: 'whiting',
  wildebeest: 'wildebeest',
  Yengeese: 'Yengeese',
  software: 'software',
  hardware: 'hardware'
}

/**
 * @var array the rules for converting a word into its plural form.
 * The keys are the regular expressions and the values are the corresponding replacements.
 */
const singulars = [
  { pattern: /([nrlm]ese|deer|fish|sheep|measles|ois|pox|media|ss)$/i, matches: '$1' },
  { pattern: /^(sea[- ]bass)$/i, matches: '$1' },
  { pattern: /(s)tatuses$/i, matches: '$1tatus' },
  { pattern: /(f)eet$/i, matches: '$1oot' },
  { pattern: /(t)eeth$/i, matches: '$1ooth' },
  { pattern: /^(.*)(menu)s$/i, matches: '$1$2' },
  { pattern: /(quiz)zes$/i, matches: '$1' },
  { pattern: /(matr)ices$/i, matches: '$1ix' },
  { pattern: /(vert|ind)ices$/i, matches: '$1ex' },
  { pattern: /^(ox)en/i, matches: '$1' },
  { pattern: /(alias)(es)*$/i, matches: '$1' },
  {
    pattern: /(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|viri?)i$/i,
    matches: '$1us'
  },
  { pattern: /([ftw]ax)es/i, matches: '$1' },
  { pattern: /(cris|ax|test)es$/i, matches: '$1is' },
  { pattern: /(shoe|slave)s$/i, matches: '$1' },
  { pattern: /(o)es$/i, matches: '$1' },
  { pattern: /ouses$/, matches: 'ouse' },
  { pattern: /([^a])uses$/, matches: '$1us' },
  { pattern: /([m|l])ice$/i, matches: '$1ouse' },
  { pattern: /(x|ch|ss|sh)es$/i, matches: '$1' },
  { pattern: /(m)ovies$/i, matches: '$1$2ovie' },
  { pattern: /(s)eries$/i, matches: '$1$2eries' },
  { pattern: /([^aeiouy]|qu)ies$/i, matches: '$1y' },
  { pattern: /([lr])ves$/i, matches: '$1f' },
  { pattern: /(tive)s$/i, matches: '$1' },
  { pattern: /(hive)s$/i, matches: '$1' },
  { pattern: /(drive)s$/i, matches: '$1' },
  { pattern: /([^fo])ves$/i, matches: '$1fe' },
  { pattern: /(^analy)ses$/i, matches: '$1sis' },
  { pattern: /(analy|diagno|^ba|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i, matches: '$1$2sis' },
  { pattern: /([ti])a$/i, matches: '$1um' },
  { pattern: /(p)eople$/i, matches: '$1$2erson' },
  { pattern: /(m)en$/i, matches: '$1an' },
  { pattern: /(c)hildren$/i, matches: '$1$2hild' },
  { pattern: /(n)ews$/i, matches: '$1$2ews' },
  { pattern: /(n)etherlands$/i, matches: '$1$2etherlands' },
  { pattern: /eaus$/, matches: 'eau' },
  { pattern: /(currenc)ies$/, matches: '$1y' },
  { pattern: /^(.*us)$/, matches: '$1' },
  { pattern: /s$/i, matches: '' }
]

/**
 * Upper case first char
 * @param {String} string
 */
export default function asSingularize(word) {
  var s = Object.keys(specials).find(k => {
    return specials[k] === word
  })
  if (s) {
    return s
  }

  var n = singulars.find(item => {
    var reg = RegExp(item.pattern)
    return reg.test(word)
  })
  if (n) {
    return word.replace(n.pattern, n.matches)
  }
  return word
}
